﻿
    using System;
    using System.Collections.Generic;

    public partial class OnlineBiz
    {
        private static DateTime _lastClearTime = DateTime.Now;
        public static Dictionary<int, OnlineMember> _onlineMember = new Dictionary<int, OnlineMember>();
        private static object _syncObj = new object();
        private static object _syncObj1 = new object();

        public static void Add(int MemberID, string SessionID, string LoginIP, string userAgent)
        {
            lock (_syncObj)
            {
                if (_onlineMember.ContainsKey(MemberID))
                {
                    _onlineMember.Remove(MemberID);
                }
                int Count = 0;
                int Key = 0;
                DateTime dtLastTime = DateTime.MaxValue;
                foreach (KeyValuePair<int, OnlineMember> kvp in _onlineMember)
                {
                    if (kvp.Value.IsNotExtrude)
                    {
                        Count++;
                    }
                    if (kvp.Value.LastNotifyTime < dtLastTime)
                    {
                        dtLastTime = kvp.Value.LastNotifyTime;
                        Key = kvp.Key;
                    }
                }
                if (((PubFunction.curParameter.RestrainOnlineNumber > 0) && (_onlineMember.Count >= PubFunction.curParameter.RestrainOnlineNumber)) && _onlineMember.ContainsKey(Key))
                {
                    _onlineMember[Key].IsNotExtrude = false;
                }
                OnlineMember member = new OnlineMember();
                member.MemberID = MemberID;
                member.SessionID = SessionID;
                member.LoginIP = LoginIP;
                member.LastNotifyTime = DateTime.Now;
                member.IsNotExtrude = true;
                member.UserAgent = userAgent;
                _onlineMember.Add(MemberID, member);
            }
        }

        private static void ClearTimeoutMember(DateTime dtNow)
        {
            int MemberLoginTimeout = 12;
            lock (_syncObj)
            {
                _lastClearTime = dtNow;
                foreach (KeyValuePair<int, OnlineMember> kvp in _onlineMember)
                {
                    TimeSpan time2 = (TimeSpan) (dtNow - kvp.Value.LastNotifyTime);
                    if (time2.TotalSeconds > MemberLoginTimeout)
                    {
                        _onlineMember.Remove(kvp.Key);
                    }
                }
            }
        }

        public static int IsValid(int MemberID, string SessionID, string userAgent)
        {
            lock (_syncObj1)
            {
                if (!_onlineMember.ContainsKey(MemberID))
                {
                    return 0;
                }
                if (!_onlineMember[MemberID].LoginIP.Equals(SessionID) || !_onlineMember[MemberID].UserAgent.Equals(userAgent))
                {
                    return 0;
                }
                if (!_onlineMember[MemberID].IsNotExtrude)
                {
                    _onlineMember.Remove(MemberID);
                    return -1;
                }
                DateTime dtNow = DateTime.Now;
                _onlineMember[MemberID].LastNotifyTime = dtNow;
                ClearTimeoutMember(dtNow);
                return 1;
            }
        }

        public static void Remove(int MemberID)
        {
            if (_onlineMember.ContainsKey(MemberID))
            {
                _onlineMember.Remove(MemberID);
            }
        }
    }


